package com.LeeCode;

import java.util.Arrays;

/**
 * 找出临界点之间的最小和最大距离
 */

public class Code2058 {
    public static void main(String[] args) {
        int[] nums = {5, 3, 1, 2, 5, 1, 2};
        ListNode head = Utils.arrayToListNode(nums);
        System.out.println(Arrays.toString(new Code2058().nodesBetweenCriticalPoints(head)));

    }

    public int[] nodesBetweenCriticalPoints(ListNode head) {
        ListNode a = head, b = a.next, c = b.next;
        int first = 0, last = 0, minDist = Integer.MAX_VALUE;
        int prev = 0, index = 1;
        while (c != null) {
            if ((a.val < b.val && b.val > c.val) || (a.val > b.val && b.val < c.val)) {
                if (first == 0) {
                    first = index;
                }
                last = index;
                if (prev != 0) {
                    minDist = Math.min(minDist, index - prev);
                }
                prev = index;
            }
            a = b;
            b = c;
            c = c.next;
            index++;
        }
        if (first == last)
            return new int[]{-1, -1};
        return new int[]{minDist, last - first};
    }
}
